/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.stock.entity.*;
import com.thinkgem.jeesite.modules.stock.service.ContainerDetailService;
import com.thinkgem.jeesite.modules.stock.service.ContainerService;
import com.thinkgem.jeesite.modules.stock.service.WarehouseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.service.MoveStockService;

import java.util.List;

/**
 * 移库Controller
 * @author 移库
 * @version 2019-12-21
 */
@Controller
@RequestMapping(value = "${adminPath}/stock/moveStock")
public class MoveStockController extends BaseController {

	@Autowired
	private MoveStockService moveStockService;
	
	@ModelAttribute
	public MoveStock get(@RequestParam(required=false) String id) {
		MoveStock entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = moveStockService.get(id);
		}
		if (entity == null){
			entity = new MoveStock();
		}
		return entity;
	}
	
	@RequiresPermissions("stock:moveStock:view")
	@RequestMapping(value = {"list", ""})
	public String list(MoveStock moveStock, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MoveStock> page = moveStockService.findPage(new Page<MoveStock>(request, response), moveStock); 
		model.addAttribute("page", page);
		return "modules/stock/moveStockList";
	}

	@Autowired
	private ContainerDetailService containerDetailService;
	@Autowired
	private ContainerService containerService;

	@RequiresPermissions("stock:moveStock:view")
	@RequestMapping(value = "view")
	public String view(MoveStock moveStock, Model model) {
		model.addAttribute("moveStock", moveStock);
		return "modules/stock/moveStockView";

	}
	@RequiresPermissions("stock:moveStock:view")
	@RequestMapping(value = "form")
	public String form(MoveStock moveStock, Model model) {
		model.addAttribute("moveStock", moveStock);
		if (org.apache.commons.lang3.StringUtils.isNotBlank(moveStock.getId())){
			// 环节编号
			String taskDefKey = moveStock.getAct().getTaskDefKey();

			if(taskDefKey.equals("audit1")){
				return "modules/stock/moveStockaudit1Form";
			}
			else if(taskDefKey.equals("audit2")){
				Container  c=new Container();
				c.setC(new WarehouseLoc(moveStock.getTlocid()));
				List<Container> ls=containerService.findList(c);
				model.addAttribute("cs", ls);
				return "modules/stock/moveStockaudit2Form";
			}
			else if(taskDefKey.equals("audit3")){

				return "modules/stock/moveStockaudit3Form";
			}
			else if(taskDefKey.equals("audit4")){

				return "modules/stock/moveStockaudit4Form";
			}
			else if(taskDefKey.equals("audit5")){

				return "modules/stock/moveStockaudit5Form";
			}
		}
		else{
			model.addAttribute("cks", warehouseService.findList(new Warehouse()));
		}

		return "modules/stock/moveStockForm";
	}

	@Autowired
	private WarehouseService warehouseService;

	@RequiresPermissions("stock:moveStock:edit")
	@RequestMapping(value = "save")
	public String save(MoveStock moveStock, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, moveStock)){
			return form(moveStock, model);
		}
		moveStockService.save(moveStock);
		addMessage(redirectAttributes, "保存移库成功");
		return "redirect:"+Global.getAdminPath()+"/stock/moveStock/?repage";
	}
	/**
	 * 工单执行（完成任务）
	 * @param model
	 * @return
	 */
	@RequiresPermissions("stock:moveStock:edit")
	@RequestMapping(value = "saveAudit")
	public String saveAudit(MoveStock pd, Model model) {
		if (org.apache.commons.lang3.StringUtils.isBlank(pd.getAct().getFlag())
				|| org.apache.commons.lang3.StringUtils.isBlank(pd.getAct().getComment())){
			addMessage(model, "请填写审核意见。");
			return form(pd, model);
		}
		moveStockService.auditSave(pd);
		return "redirect:" + adminPath + "/act/task/todo/";
	}

	@RequiresPermissions("stock:moveStock:edit")
	@RequestMapping(value = "delete")
	public String delete(MoveStock moveStock, RedirectAttributes redirectAttributes) {
		moveStockService.delete(moveStock);
		addMessage(redirectAttributes, "删除移库成功");
		return "redirect:"+Global.getAdminPath()+"/stock/moveStock/?repage";
	}

}